package cn.tedu.route;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

public class RouteProducer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.通过连接工厂对象配置连接信息
        ConnectionFactory cf = new ConnectionFactory();
        cf.setHost("172.26.6.53");//写成自己的ip
        cf.setPort(5672); //5672是客户端端口，15672是UI界面端口，25672是erlang发现口
        cf.setUsername("guest");
        cf.setPassword("guest");
//        cf.setVirtualHost("/test");
        //2.创建tcp和channel
        Connection nc = cf.newConnection();
        Channel cc = nc.createChannel();
        //3.创建一个fanout类型的交换机
        cc.exchangeDeclare("logs", BuiltinExchangeType.DIRECT);
        //4.向交换机发送数据
        while (true){
            Scanner scanner = new Scanner(System.in);
            System.out.print("输入消息:");
            String message = scanner.nextLine();
            System.out.print("输入路由键:");
            String routingKey = scanner.nextLine();
            if (message.equals("quit"))
                break;
            cc.basicPublish("logs",routingKey, null ,message.getBytes());
        }
        nc.close();
    }
}
